home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 117 (1989-11-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 117 (1989-11-15)(Ossowski, Stefan)(DE)(PD).adf / LeftyMouse / LeftyMouse.c < prev    next >
C/C++ Source or Header  |  1989-08-20  |  5KB  |  111 lines

  1. /*****************************************************************
  2.  *
  3.  *           LeftyMouse - make your Amiga ambidextrous
  4.  *
  5.  *  This program will make your mouse left-handed by installing
  6.  *  an input handler which swaps the functions of the left
  7.  *  and right mouse buttons.
  8.  *
  9.  *****************************************************************/
  10. #include <intuition/intuitionbase.h>
  11. #include <devices/input.h>
  12. #include <proto/exec.h>
  13. #include <proto/intuition.h>
  14.  
  15. #define PORTNAME "-<:RBE_Lefty:>-"
  16.  
  17. struct Interrupt handlerptr;           /* interrupt struct to install handler */
  18. struct MsgPort *inputport, *replyport; /* msg ports for communications */
  19. struct IOStdReq *inputreq;             /* IO request for input device */
  20. struct Message kill;                   /* message to kill if already running */
  21. extern struct IntuitionBase *IntuitionBase;
  22. extern void hndstub();                 /* assembly interface stub */
  23. struct InputEvent *hndcode(struct InputEvent *,int); /* input handler func */
  24.  
  25. void _main()   /* name is _main() not main() to save space (Lattice) */
  26. {
  27.   inputport = FindPort(PORTNAME);   /*  does port already exist -   */
  28.   if (inputport == NULL)            /*  is program already running? */
  29.   {                                 /*  no - install input handler  */
  30.     IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0);
  31.     inputport = CreatePort(PORTNAME,0);  /* make port for IO request */
  32.     if (IntuitionBase== NULL || inputport==NULL)
  33.       exit(20);                          /* bomb if can't open */
  34.     inputreq = CreateStdIO(inputport);   /* open IO request    */
  35.     if (inputreq == NULL)
  36.       exit(21);                          /* bomb if can't open */
  37.     if (OpenDevice("input.device",0,(struct IORequest *)inputreq,0) != NULL)
  38.       exit(22);                /* bomb if can't open input device */
  39.  
  40.     handlerptr.is_Code = hndstub;     /* set up interrupt struct to  */
  41.     handlerptr.is_Data = NULL;        /* install input handler       */
  42.     handlerptr.is_Node.ln_Pri = 127;  /* maximum priority (I think)  */
  43.  
  44.     inputreq->io_Command = IND_ADDHANDLER;
  45.     inputreq->io_Data = (APTR)&handlerptr;  /* IO request to input device to */
  46.     DoIO((struct IORequest *)inputreq);     /* install input handler         */
  47.  
  48. /*  At this point, the input handler is installed and doing its thing.
  49.     However, we can't leave now because the handler itself would also
  50.     be unloaded.  So, we will wait for any message (or possibly forever).
  51.     If we get a message we will de-install the handler and leave.  */
  52.  
  53.     ReplyMsg(WaitPort(inputport));    /*  wait for any message  */
  54.  
  55.     inputreq->io_Command = IND_REMHANDLER;  /* start packing up     */
  56.     DoIO((struct IORequest *)inputreq);     /* remove input handler */
  57.     CloseDevice((struct IORequest *)inputreq);  /* close and delete     */
  58.     DeleteStdIO(inputreq);                      /* everything we opened */
  59.     DeletePort(inputport);                      /* and allocated        */
  60.     CloseLibrary((struct Library *)IntuitionBase);
  61.     exit(0);                 /* now we leave! */
  62.   }
  63.  
  64.   else    /*  program already running - send message to kill it  */
  65.   {
  66.     replyport = CreatePort(0,0);     /* create reply port for kill msg */
  67.     if (replyport == NULL)
  68.       exit(20);                      /* bomb if can't create  */
  69.     kill.mn_ReplyPort = replyport;
  70.     kill.mn_Length = 0;
  71.     PutMsg(inputport,&kill);  /* send kill msg to program already running */
  72.     while(WaitPort(replyport) != &kill)  /* wait for reply */
  73.     {}
  74.     DeletePort(replyport);    /* clean up */
  75.     exit(100);                /* bye!     */
  76.   }
  77.     
  78. }
  79. /*  This is the actual input handler code.  It is called by the assembly
  80.     stub hndstub(), which is called whenever there is an input event */
  81.  
  82. struct InputEvent *hndcode(ie,data)
  83.  
  84. register struct InputEvent *ie;
  85. int data;
  86. {
  87.   register unsigned short x;     /* to hold input event code */
  88.   register struct InputEvent *e;
  89.  
  90.   e = ie;
  91.   do
  92.   {
  93.              /* is event a mouse button?  */
  94.     if (e->ie_Class==IECLASS_RAWMOUSE && e->ie_Code!=IECODE_NOBUTTON)
  95.     {
  96.       x = e->ie_Code & 0x007f;   /* mask off IECODE_UP_PREFIX for now */
  97.  
  98.    /* swap button codes, and put back IECODE_UP_PREFIX bit setting */
  99.       if (x == IECODE_LBUTTON)
  100.         e->ie_Code = IECODE_RBUTTON | (e->ie_Code & IECODE_UP_PREFIX);
  101.       else if (x == IECODE_RBUTTON)
  102.         e->ie_Code = IECODE_LBUTTON | (e->ie_Code & IECODE_UP_PREFIX);
  103.     }
  104.     e = e->ie_NextEvent;
  105.   }  while (e != NULL);  /* go thru entire event list */
  106.  
  107.   return(ie);  /* pass event(s) on down the line */
  108. }
  109.  
  110. void MemCleanup() {}  /* dummy function to save space (Lattice) */
  111.